<?php
namespace app\center\controller;
use app\center\logic\SourceLogic;

/**
 * 数据源api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/10
 * Time: 11:12
 */
class Source extends Base {

    public function __construct(){
        parent::__construct();
        $this->s_logic = new SourceLogic();
    }

    /**
     * 获取事项数据源详情
     * @return \think\response\Json
     */
    public function ajax_get_matter(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->s_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = [];
        $maps[] = ['id','=',$params['matterid']];
        $m_detail = model('matter')->get_detail($maps,'id,from_openid,sourceid,source,name,out_time,c_time,deal_openid');

        //获取事项信息
        //查找发起人
        $maps = [];
        $maps[] = ['openid','=',$m_detail['from_openid']];
        $from_u_detail = model('user')->get_detail($maps,'id,username');

        //查找处理人
        $maps = [];
        $maps[] = ['openid','=',$m_detail['deal_openid']];
        $deal_u_detail = model('user')->get_detail($maps,'id,username');

        $matter_info = [];
        $matter_info['is_matter'] = 1;
        $matter_info['from_username'] = $from_u_detail['username'];
        $matter_info['deal_username'] = $deal_u_detail['username'];
        $matter_info['source_zw'] = $m_detail->source_name;
        $matter_info['out_time'] = $m_detail['out_time'];
        $matter_info['c_time'] = $m_detail['c_time'];

        //获取来源信息
        $s = new \app\common\controller\Source();
        $r = $s->get_source($m_detail['sourceid'],$m_detail['source']);
        if(empty($r)){
            return json($this->s_logic->get_response(0,'该事项不存在'));
        }
        $source_info = $r['source_info'];
        $source_info['name'] = $m_detail['name'];

        //获取事项处理动态
        $m = new \app\common\controller\Matter();
        $deal = $m->get_matter_deal($m_detail['id']);
        $deal_info = $deal['deal_info'];

        //获取事项处理动作
        $action = $m->get_matter_action($u_detail['openid'],$m_detail['id']);
        $action_info['action'] = $action['action_info'];

        $data['matter_info'] = $matter_info;
        $data['source_info'] = $source_info;
        $data['deal_info'] = $deal_info;
        $data['action_info'] = $action_info;
        return json($this->s_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取抄送数据源详情
     * @return \think\response\Json
     */
    public function ajax_get_copy(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->s_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = [];
        $maps[] = ['id','=',$params['copyid']];
        $c_detail = model('copy')->get_detail($maps,'id,from_openid,sourceid,source,name,explain,c_time');

        //获取抄送人
        $maps = [];
        $maps[] = ['openid','=',$c_detail['from_openid']];
        $c_u_detail = model('user')->get_detail($maps,'id,username');
        $copy_info = [];
        $copy_info['username'] = !empty($c_u_detail) ? $c_u_detail['username'] : '';
        $copy_info['c_time'] = $c_detail['c_time'];

        //获取来源信息
        $s = new \app\common\controller\Source();
        $r = $s->get_source($c_detail['sourceid'],$c_detail['source']);
        if(empty($r)){
            return json($this->s_logic->get_response(0,'该抄送不存在'));
        }
        $source_info = $r['source_info'];
        $source_info['name'] = $c_detail['name'];

        //获取事项信息
        $maps = [];
        $maps[] = ['sourceid','=',$c_detail['sourceid']];
        $maps[] = ['source','=',$c_detail['source']];
        $m_detail = model('matter')->get_detail($maps,'id,from_openid,sourceid,source,name,out_time,c_time,deal_openid');
        if(!empty($m_detail)){
            //查找发起人
            $maps = [];
            $maps[] = ['openid','=',$m_detail['from_openid']];
            $from_u_detail = model('user')->get_detail($maps,'id,username');

            //查找处理人
            $maps = [];
            $maps[] = ['openid','=',$m_detail['deal_openid']];
            $deal_u_detail = model('user')->get_detail($maps,'id,username');

            $matter_info = [];
            $matter_info['is_matter'] = 1;
            $matter_info['from_username'] = $from_u_detail['username'];
            $matter_info['deal_username'] = $deal_u_detail['username'];
            $matter_info['source_zw'] = $m_detail->source_name;
            $matter_info['out_time'] = $m_detail['out_time'];
            $matter_info['c_time'] = $m_detail['c_time'];

            //获取事项处理动态
            $m = new \app\common\controller\Matter();
            $deal = $m->get_matter_deal($m_detail['id']);
            $deal_info = $deal['deal_info'];

            //获取事项处理动作
            $action = $m->get_matter_action($u_detail['openid'],$m_detail['id']);
            $action_info['action'] = $action['action_info'];
        }else{
            $matter_info = [];
            $matter_info['is_matter'] = 0;

            $deal_info = [];
            $action_info = [];
        }

        //获取批复
        if($params['menu_type'] == 4){
            $reply_info = [];
            $reply_info['explain'] = $c_detail['explain'];
        }elseif($params['menu_type'] == 5){
            $r = $this->s_logic->get_reply($u_detail['openid'],$c_detail['sourceid'],$c_detail['source'],$params['menu_type']);
            $reply_info = $r['data']['list'];
        }

        //更新抄送为已读
        $r = $this->s_logic->save_copy_read($c_detail['id'],$params['menu_type']);

        //更新批复为已读
        $r = $this->s_logic->save_reply_read($u_detail['openid'],$c_detail['sourceid'],$c_detail['source'],$params['menu_type']);

        $data['copy_info'] = $copy_info;
        $data['matter_info'] = $matter_info;
        $data['source_info'] = $source_info;
        $data['deal_info'] = $deal_info;
        $data['action_info'] = $action_info;
        $data['reply_info'] = $reply_info;
        return json($this->s_logic->get_response(1,'OK',$data));
    }
}
